<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getEquipList()">
            <el-button slot="append" icon="el-icon-search" @click="getEquipList()"></el-button>
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加设备</el-button>
        </el-col>
      </el-row>
      <!-- 产品列表 -->
      <el-table :data="equipList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="设备编号" prop="eq_code"></el-table-column>
        <el-table-column label="设备名称" prop="eq_name"></el-table-column>
        <el-table-column label="设备规格" prop="eq_spec"></el-table-column>
        <el-table-column label="设备种类" prop="eq_cata"></el-table-column>
        <el-table-column label="租用状态" prop="eq_rent"></el-table-column>
        <el-table-column label="设备状态" prop="eq_state"></el-table-column>
        <el-table-column label="所属工厂ID" prop="eq_fac_id"></el-table-column>
        <el-table-column label="操作" fixed="right" width="220">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.eq_id)" circle></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" @click="deleteEquip(scope.row.eq_id)" circle></el-button>
            <!-- 开启 -->
            <el-button type="success" icon="el-icon-check" @click="startEquip(scope.row.eq_id)" circle></el-button>
            <!-- 关闭 -->
            <el-button type="warning" icon="el-icon-close"  @click="shutEquip(scope.row.eq_id)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>

    <!-- 创建新产品对话框 -->
    <el-dialog title="添加设备" :visible.sync="addDialogVisible" width="50%"
               @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules"
               ref="addFormRef" label-width="70px">
        <!-- 产品名称 -->
        <el-form-item label="设备名" prop="eq_name">
          <el-input v-model="addForm.eq_name"></el-input>
        </el-form-item>

        <!-- 产品规格 -->
        <el-form-item label="规格" prop="eq_spec">
          <el-input v-model="addForm.eq_spec"></el-input>
        </el-form-item>

        <!-- 产品种类 -->
        <el-form-item label="类别" prop="eq_cata">
          <el-select v-model="addForm.eq_cata">
            <el-option :value="equipType.eq_ca_name" v-for="equipType in equipTypeList">{{equipType.eq_ca_name}}</el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEquip">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改产品类别对话框 -->
    <el-dialog title="修改产品信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
      <el-form :model="editForm"  ref="editFormRef" label-width="70px">
        <el-form-item prop="eq_id" hidden>
          <el-input v-model="editForm.eq_id" hidden></el-input>
        </el-form-item>
        <!-- 产品名称 -->
        <el-form-item label="设备名" prop="eq_name">
          <el-input v-model="editForm.eq_name"></el-input>
        </el-form-item>

        <!-- 产品规格 -->
        <el-form-item label="规格" prop="eq_spec">
          <el-input v-model="editForm.eq_spec"></el-input>
        </el-form-item>

        <!-- 产品种类 -->
        <el-form-item label="类别" prop="eq_cata">
          <el-select v-model="editForm.eq_cata">
            <el-option :value="equipType.eq_ca_name" v-for="equipType in equipTypeList">{{equipType.eq_ca_name}}</el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editEquipInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5
      },
      equipList: [],// 用户列表
      equipTypeList:['北京','上海'],

      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
      // 添加产品类别表单项
      addForm: {
        eq_id:'',
        eq_name:'',
        eq_code:'',
        eq_spec:'',
        eq_state:'0',
        eq_rent:'0',
        eq_fac_id:'0',
        eq_cata:''
      },
      // 控制修改用户对话框显示/隐藏
      editDialogVisible:false,
      // 修改用户信息
      editForm:{},
      addFormRules:[],
    };
  },
  created() {
    this.getEquipList();
    this.getEquipTypeList();
  },
  methods: {
    async getEquipList() {
      // 调用post请求
      const { data: res } = await this.$http.get("equip/equipList", {
        params: this.queryInfo
      });
      for(var i in res.data){
        if(res.data[i].eq_state === 1){
          res.data[i].eq_state = '已开启';
        }
        if(res.data[i].eq_state === 0){
          res.data[i].eq_state = '已关闭';
        }

        if(res.data[i].eq_rent === 1){
          res.data[i].eq_rent = '已租用';
        }
        if(res.data[i].eq_rent === 0){
          res.data[i].eq_rent = '未租用';
        }
        if(res.data[i].eq_rent === -1){
          res.data[i].eq_rent = '工厂设备';
        }

        if(res.data[i].eq_fac_id === 0){
          res.data[i].eq_fac_id = '闲置中';
        }else{
          var temp_fac_id = res.data[i].eq_fac_id;
          res.data[i].eq_fac_id = '工厂ID:    ' + temp_fac_id;
        }
      }
      this.equipList = res.data; // 将返回数据赋值
      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getEquipList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getEquipList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    // 添加产品类别
    addEquip(){
      this.getEquipTypeList();
      this.$refs.addFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("equip/addEquip",this.addForm);
        if (res !== "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getEquipList();
      })
    },
    async getEquipTypeList(){
      const { data: res } = await this.$http.get("equip/allEquipTypeList");
      this.equipTypeList = res.data;
      console.log(this.equipTypeList);
    },
    // 展示修改框
    async showEditDialog(id){

      const {data:res} = await this.$http.post("equip/getUpdate?id="+id);
      // if (res != "success") {
      // userinfo.state = !userinfo.state;
      // return this.$message.error("操作失败！！！");
      // }
      // this.$message.success("操作成功！！！");

      this.editForm = res;
      this.editDialogVisible = true;
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editEquipInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("equip/editEquip",this.editForm);
        console.log(res);
        if (res !== "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getEquipList();
      });
    },
    // 删除按钮
    async deleteEquip(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.post("equip/deleteEquip?id="+id);
      if (res !== "success") {
        return this.$message.error("工厂自有设备和租用设备，禁止删除");
      }
      this.$message.success("操作成功！！！");
      this.getEquipList();
    },
    //关闭设备
    async shutEquip(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将关闭该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消关闭");
      }
      const {data:res} = await this.$http.post("equip/shutEquip?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getEquipList();
    },
    //开启设备
    async startEquip(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将开启该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消开启");
      }
      const {data:res} = await this.$http.post("equip/startEquip?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getEquipList();
    },
  },
};
</script>
<style lang="less" scoped>
</style>